package pers.hl.module_main.mine

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import pers.hl.common.base.ext.showMessage
import pers.hl.lib_global.common.GlideUtils
import pers.hl.lib_global.model.bean.Comment
import pers.hl.lib_global.model.bean.Reply
import pers.hl.module_main.BaseTitleRvFragment
import pers.hl.module_main.R
import pers.hl.module_main.common.BaseLibQuickAdapter
import pers.hl.module_main.common.CommonDialog
import pers.hl.module_main.databinding.ItemRvCommentBinding

class MyCommentFragment : BaseTitleRvFragment<Comment>(){

    override fun getTitle(): String {
        return "我的评论"
    }

    override fun beforeSetAdapter() {
//        mBinding.apply {
//            val creator = SwipeMenuHelper.createItem(mContext, "删除评论")
//            recyclerView.setSwipeMenuCreator(creator)
//            recyclerView.setOnItemMenuClickListener { menuBridge, adapterPosition ->
//
//                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
//                menuBridge.closeMenu()
//                val item = mAdapter.getItem(adapterPosition) as Comment
//                ConfirmDialog.create(mContext)
//                    .content("您确定要删除评论吗？")
//                    .setConfirmListener {
//                        item.cmtId?.let { mViewModel.deleteComment(it).observe(this@MyCommentFragment, Observer {
//                            showMessage("评论删除成功")
//                            mAdapter.removeAt(adapterPosition)
//                        }) }
//                    }
//                    .setCancelListener {  }
//                    .show()
//            }
//        }
    }

    override fun createAdapter(): BaseLibQuickAdapter<Comment, BaseViewHolder> {
        return object : BaseLibQuickAdapter<Comment, BaseViewHolder>(R.layout.item_rv_comment) {
            override fun convert(holder: BaseViewHolder, item: Comment) {
                val binding = ItemRvCommentBinding.bind(holder.itemView)
                binding.apply {
                    item.cmtUserId?.let {
                        GlideUtils.loadUserAvatar(
                            ivAvatar.context, ivAvatar,
                            it, R.drawable.ic_personal_center
                        )
                        tvCommenter.text =
                            if (TextUtils.isEmpty(item.userName)) "匿名用户" else item.userName
                    }
                    tvTime.text = TimeUtils.date2String(item.cmtTime)
                    tvComment.text = item.cmtInfo
                    tvReplyCount.text = String.format("共%s条回复 >", item.replyCount.toString())
                    tvReplyCount.visibility = if (item.replyCount > 0) View.VISIBLE else View.GONE
                    tvReplyCount.setOnClickListener {
                        fetchReplyList(item)
                    }
                }
            }
        }
    }

    fun fetchReplyList(comment: Comment) {
        comment.cmtId?.let {
            mViewModel.fetchReplyList(it).observe(this, Observer { list ->
                if (list == null || list.isEmpty()) {
                    showMessage("暂无回复")
                    return@Observer
                }
                showReplyListDialog(list)
            })
        }
    }

    /**
     * 查看回复列表弹框
     */
    private fun showReplyListDialog(list: List<Reply>) {
        CommonDialog.createBottomList(
            mContext,
            list.toMutableList(),
            R.layout.item_rv_comment,
            convertBlock = { holder, item ->
                val binding = ItemRvCommentBinding.bind(holder.itemView)
                binding.apply {
                    item.rplUserId?.let {
                        GlideUtils.loadUserAvatar(
                            mContext, binding.ivAvatar,
                            it, R.drawable.ic_personal_center
                        )
                    }
                    tvCommenter.text =
                        if (TextUtils.isEmpty(item.rplUserName)) "匿名用户" else item.rplUserName
                    tvTime.text = TimeUtils.date2String(item.rplTime)
                    tvComment.text = item.rplInfo
                }
            },
            onItemClick = { layer, adapter, position, item ->
                LogUtils.d("点击回复")
            }).show()
    }

    override fun initData(savedInstanceState: Bundle?) {
        mViewModel.fetchMyComment().observe(this, Observer {
            if (it.isNullOrEmpty()) {
                mAdapter.setEmptyView()
            } else {
                mAdapter.setList(it)
            }
        })
    }
}